package com.unbook.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.unbook.entity.Book;
import com.unbook.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

public interface IBookMapper extends BaseMapper<Book> {
    @Select("select * from book")
    @Results(value = {
        @Result(id = true, property = "bId", column = "b_id"),
        @Result(property = "name", column = "name"),
        @Result(property = "title", column = "title"),
        @Result(property = "intro", column = "intro"),
        @Result(property = "price", column = "price"),
        @Result(property = "createTime", column = "create_time", javaType = Date.class, jdbcType = JdbcType.DATETIMEOFFSET),
        @Result(property = "updateTime", column = "update_time", javaType = Date.class, jdbcType = JdbcType.DATETIMEOFFSET),
        @Result(property = "typeList", column = "b_id", javaType = List.class,
            many = @Many(select = "com.unbook.mapper.IBookTypeMapper.findByBookId")),
        @Result(property = "user", column = "u_id", javaType = User.class, one = @One(select = "com.unbook.mapper.IUserMapper.selectById"))
    })
    IPage<Book> selectBookOnType(IPage<Book> page, @Param(Constants.WRAPPER) Wrapper<Book> wrapper);

    @Select("select * from book")
    @Results(value = {
        @Result(id = true, property = "bId", column = "b_id"),
        @Result(property = "name", column = "name"),
        @Result(property = "title", column = "title"),
        @Result(property = "intro", column = "intro"),
        @Result(property = "price", column = "price"),
        @Result(property = "explain", column = "explain"),
        @Result(property = "createTime", column = "create_time", javaType = Date.class, jdbcType = JdbcType.DATETIMEOFFSET),
        @Result(property = "updateTime", column = "update_time", javaType = Date.class, jdbcType = JdbcType.DATETIMEOFFSET),
        @Result(property = "typeList", column = "b_id", javaType = List.class,
            many = @Many(select = "com.unbook.mapper.IBookTypeMapper.findByBookId")),
        @Result(property = "user", column = "u_id", javaType = User.class, one = @One(select = "com.unbook.mapper.IUserMapper.selectById"))
    })
    Book selectBookById(Integer bid);
}
